package com.engine4cloud.tdf.education.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.data.web.query.Queries;
import com.engine4cloud.tdf.common.security.service.TdfUser;
import com.engine4cloud.tdf.common.security.util.SecurityUtils;
import com.engine4cloud.tdf.education.convert.EduCourseResourceConvert;
import com.engine4cloud.tdf.education.entity.EduCourseResourceEntity;
import com.engine4cloud.tdf.education.mapper.EduCourseResourceMapper;
import com.engine4cloud.tdf.education.service.EduCourseResourceService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 学习资源-文件表
 *
 * @author wenjie
 * @date 2024-07-31 16:57:43
 */
@Service
public class EduCourseResourceServiceImpl extends ServiceImpl<EduCourseResourceMapper, EduCourseResourceEntity> implements EduCourseResourceService {
    @Override
    public List<EduCourseResourceEntity> findList(String courseCode) {
        LambdaQueryWrapper<EduCourseResourceEntity> wrapper = Queries.lambda();
        wrapper.eq(EduCourseResourceEntity::getCourseCode, courseCode);
        return super.list(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchCreate(String courseCode, List<String> resourceList) {
        if (CollectionUtils.isEmpty(resourceList)) {
            return;
        }

        TdfUser user = SecurityUtils.getUser();
        super.saveBatch(resourceList.stream().map(r -> EduCourseResourceConvert.INSTANCE.create(courseCode, r, user.getTenantId())).toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchUpdate(String courseCode, List<String> resourceList) {
        TdfUser user = SecurityUtils.getUser();
        List<EduCourseResourceEntity> existedList = this.findList(courseCode);
        List<EduCourseResourceEntity> preSaveList = this.preSaveList(courseCode, resourceList, existedList, user);
        if (CollectionUtils.isNotEmpty(preSaveList)) {
            super.saveBatch(preSaveList);
        }

        List<Long> preDeleteList = this.preDeleteList(resourceList, existedList);
        if (CollectionUtils.isNotEmpty(preDeleteList)) {
            super.removeBatchByIds(preDeleteList);
        }
    }

    private List<EduCourseResourceEntity> preSaveList(String courseCode, List<String> resourceList, List<EduCourseResourceEntity> existedList, TdfUser user) {
        if (CollectionUtils.isEmpty(resourceList)) {
            return Collections.emptyList();
        }

        Map<String, EduCourseResourceEntity> existedMap = existedList.stream().collect(Collectors.toMap(EduCourseResourceEntity::getResourceCode, v -> v));

        List<EduCourseResourceEntity> preSaveList = new ArrayList<>();

        resourceList.forEach(r -> {
            if (!existedMap.containsKey(r)) {
                EduCourseResourceEntity entity = EduCourseResourceConvert.INSTANCE.create(courseCode, r, user.getTenantId());
                preSaveList.add(entity);
            }
        });

        return preSaveList;
    }

    private List<Long> preDeleteList(List<String> resourceList, List<EduCourseResourceEntity> existedList) {
        if (CollectionUtils.isEmpty(resourceList)) {
            return Collections.emptyList();
        }

        Map<String, EduCourseResourceEntity> existedMap = existedList.stream().collect(Collectors.toMap(EduCourseResourceEntity::getResourceCode, v -> v));

        List<Long> preDeleteList = new ArrayList<>();

        resourceList.forEach(r -> {
            if (existedMap.containsKey(r)) {
                preDeleteList.add(existedMap.get(r).getId());
            }
        });

        return preDeleteList;
    }

}